package com.aaa.sbm.service.impl;

import com.aaa.sbm.dao.DeptDao;
import com.aaa.sbm.entity.Dept;
import com.aaa.sbm.service.DeptService;
import com.aaa.sbm.util.CustomException;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @FileName: DeptServiceImpl
 * @Description:
 * @Author: zhz
 * @CreateTime: 2025/8/21 11:03
 * @Version: 1.0.0
 */
@Service
public class DeptServiceImpl  implements DeptService {

    @Resource
    private DeptDao deptDao;

//    @Override
//    public List<Dept> queryByParam(Dept dept) {
//        return deptDao.queryByParam(dept);
//    }


    @Override
    public PageInfo queryByPage(Page<Dept> page) {
        //获取
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();
        //判断pageNo和pageSize不能为空和0
        if(pageNo==null||pageNo==0||pageSize==null||pageSize==0){
            //抛出异常，代码终止
            //throw  new IllegalArgumentException("非法参数异常1");
            //抛出自定义异常
            throw  new CustomException(ResultStatus.PARAM_IS_NULL.getReturnCode()
                    ,ResultStatus.PARAM_IS_NULL.getReturnMessage());
        }
       // System.out.println(1/0);
        //调用分页插件中的方法，传入页码和每页显示数量，底层要进行分页
        PageHelper.startPage(pageNo,pageSize);
        //获取查询参数
        Dept data = page.getData();
        //执行查询，并且实例化PageInfo返回结果   pageInfo有list属性  就是List<Dept>
        return new PageInfo(deptDao.queryByParam(data));
    }

    @Override
    public Dept queryById(Integer deptNo) {
        return deptDao.queryById(deptNo);
    }

    @Override
    public int add(Dept dept) {
        return deptDao.add(dept);
    }

    @Override
    public int update(Dept dept) {
        return deptDao.update(dept);
    }

    @Override
    public int deleteById(Integer deptNo) {
        return deptDao.deleteById(deptNo);
    }
}
